'use strict';

const express = require('express');
const dbutility = require('../../common/dbutility');
const gputility = require('../../common/gputility');
const logger = require('../../common/logutility').logger('regCouConf');
let router = express.Router();

router.get('/', function(req, res) {
    gputility.excuteGP(getGP(req, res));
});

router.post('/', function(req, res) {
    gputility.excuteGP(postGP(req, res));
});

function* getGP(req, res) {
    try {
        res.render('./adm/regCouConf', req.session.couReg);
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

function* postGP(req, res) {
    try {
        let couName = req.session.couReg.couName;
        let couTel = req.session.couReg.couTel;
        let couCmp = req.session.couReg.couCmp;
        yield dbutility.couModel.insertCou(couName, couTel, couCmp);

        logger.info('Reg Cou Info:');
        logger.info(req.session.couReg);

        res.redirect('/regCouCmp');
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

module.exports = router;